package com.ecoolex.discount.sales.admin.graphql;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs;
import com.ecoolex.discount.sales.entity.UPlanCouponBatch;
import com.ecoolex.discount.sales.pojo.AddCouponBatchDTO;
import com.ecoolex.discount.sales.pojo.FetchCouponDTO;
import com.ecoolex.discount.sales.pojo.QueryCouponBatchDTO;
import com.ecoolex.discount.sales.ws.CouponBatchService;
import com.ecoolex.discount.sales.ws.CouponService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "优惠券批次管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY, key = CouponBatchGraphql.AUTH, name = "优惠券批次管理", remark = "优惠券批次管理")
public class CouponBatchGraphql {

	public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-COUPON-BATCH";

	@Reference
	CouponBatchService couponBatchService;

	@Reference
	CouponService couponService;

	public static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "couponBatchList",description = "优惠券批次列表")
	@AdminAuthoritativeRegister(parentKey = AUTH,key = AUTH_QUERY,name = "优惠券批次列表")
	public Pagination<DiscountBrandWithBLOBs> couponBatchList(@NonNull QueryCouponBatchDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return couponBatchService.couponBatchList(query,sorts, page);
	}

	public static final String AUTH_ADD = AUTH + "-ADD";

	@MutationField(field = "addCouponBatch", description = "添加优惠券批次")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加优惠券批次")
	public UPlanCouponBatch addCouponBatch(@NonNull AddCouponBatchDTO dto, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		String batchNo = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-ddHHmmss"));
		return couponBatchService.addBatch(dto, batchNo, user.userName());
	}


	public static final String AUTH_FETCH_COUPON = AUTH + "-FETCH-COUPON";

	@MutationField(field = "fetchCoupon", description = "根据批次号获取优惠券")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_FETCH_COUPON, name = "根据批次号获取优惠券")
	public Boolean fetchCoupon(@NonNull FetchCouponDTO dto, AdminMemberAuthority user) {
		user.assertAuth(AUTH_FETCH_COUPON);
		couponService.fetch(dto.getBatchId(), dto.getAmount());
		return Boolean.TRUE;
	}
}
